home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / elm / elm2.4 / lib / safemalloc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-11  |  1.8 KB  |  74 lines

  1.  
  2. static char rcsid[] = "@(#)$Id: safemalloc.c,v 5.1 1993/04/12 01:51:01 syd Exp $";
  3.  
  4. /*******************************************************************************
  5.  *  The Elm Mail System  -  $Revision: 5.1 $   $State: Exp $
  6.  *
  7.  *             Copyright (c) 1992 USENET Community Trust
  8.  *******************************************************************************
  9.  * Bug reports, patches, comments, suggestions should be sent to:
  10.  *
  11.  *    Syd Weinstein, Elm Coordinator
  12.  *    elm@DSI.COM            dsinc!elm
  13.  *
  14.  *******************************************************************************
  15.  * $Log: safemalloc.c,v $
  16.  * Revision 5.1  1993/04/12  01:51:01  syd
  17.  * Initial Checkin
  18.  *
  19.  *
  20.  ******************************************************************************/
  21.  
  22. #include <stdio.h>
  23. #include "defs.h"
  24.  
  25. /*
  26.  * These routines perform dynamic memory allocation with error checking.
  27.  * The "safe_malloc_fail_handler" vector points to a routine that is invoked
  28.  * if memory allocation fails.  The default error handler displays a message
  29.  * and aborts the program.
  30.  */
  31.  
  32.  
  33. void dflt_safe_malloc_fail_handler(proc, len)
  34. char *proc;
  35. unsigned len;
  36. {
  37.     fprintf(stderr,
  38.         "error - out of memory [%s failed allocating %d bytes]\n",
  39.         proc, len);
  40.     exit(1);
  41. }
  42.  
  43. void (*safe_malloc_fail_handler)() = dflt_safe_malloc_fail_handler;
  44.  
  45.  
  46. malloc_t safe_malloc(len)
  47. unsigned len;
  48. {
  49.     malloc_t p;
  50.     if ((p = malloc(len)) == NULL)
  51.         (*safe_malloc_fail_handler)("safe_malloc", len);
  52.     return p;
  53. }
  54.  
  55.  
  56. malloc_t safe_realloc(p, len)
  57. malloc_t p;
  58. unsigned len;
  59. {
  60.     if ((p = (p == NULL ? malloc(len) : realloc((malloc_t)p, len))) == NULL)
  61.         (*safe_malloc_fail_handler)("safe_realloc", len);
  62.     return p;
  63. }
  64.  
  65.  
  66. char *safe_strdup(s)
  67. char *s;
  68. {
  69.     char *p;
  70.     if ((p = (char *) malloc(strlen(s)+1)) == NULL)
  71.         (*safe_malloc_fail_handler)("safe_strdup", strlen(s)+1);
  72.     return strcpy(p, s);
  73. }
  74.